var common = require('../../common.js');
var app = getApp();
Page({
  data:{
    userinfo: null,
    appinfo:{},
    page:1,
    items:[],
    wxcode: '',
    loading: 3
  },
  onLoad:function(){
    this.infoLoad();
    this.dataLoad();
    this.getWxCode();
    this.setData({appinfo:app.globalData.appInfo});
    
  },
  getWxCode: function () {
    wx.login({
      success: res => {
        this.setData({
          wxcode: res.code
        });
      }
    });
  },
  onPullDownRefresh:function(){
    this.setData({page:1,loading:0,items:[]});
    this.dataLoad();
    this.infoLoad();
  },
  infoLoad:function(){
    this.data.userinfo = wx.getStorageSync('userInfo');
    console.info(this.data.userinfo)
    if(!this.data.userinfo.openid){
      return false;
    }else{
      this.setData({ userinfo: this.data.userinfo });
    }
    wx.request({
      url: app.globalData.baseUrl +"/blade-system/routineuser/detail",
      method: 'get',
      data: {
        token:common.token,
        appid:wx.getAccountInfoSync().miniProgram.appId,
        openid: this.data.userinfo.openid
      },
      success: res => {
        this.setData({userinfo:res.data.data});

      }
    });
  },
  dataLoad:function(){
    this.infoLoad();
    if(!this.data.userinfo.openid){
      return;
    }
    this.setData({loading:1});
    wx.request({
      url: app.globalData.baseUrl + "/blade-video/userlike/favorite?current="+ this.data.page,
      method: 'POST',
      data: {
        token:common.token,
        current: this.data.page,
        appId:wx.getAccountInfoSync().miniProgram.appId,
        openid: this.data.userinfo.openid
      },
      success: res => {
        wx.stopPullDownRefresh();
        if (res.data.code == 200) {
          this.setData({loading:0});
          this.data.items = this.data.items.concat(res.data.data.records);
          this.data.page++;
          this.setData({items:this.data.items,page:this.data.page});
        }else if(res.data.code == 888){
          this.data.items = this.data.items.concat(res.data.data);
          this.data.page++;
          this.setData({items:this.data.items,page:this.data.page});
          this.setData({loading:2});
        }else if(res.data.code == 999){
          this.setData({loading:4});
        }
      }
    });
  },
  openVideo:function(option){
    wx.navigateTo({
      url: '../media/detail?video_id=' +  option.currentTarget.dataset.vid,
    });
  },
  // 滚动加载
  onReachBottom:function(){
      if (this.data.loading == 1 || this.data.loading == 2) { return; }
      this.dataLoad();
  },
  unLike:function(option){
    var index = option.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定要将这个视频从收藏移除吗?',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: app.globalData.baseUrl + "/blade-video/userlike/like",
            method: 'POST',
            data: {
              token:common.token,
              openid: wx.getStorageSync('userInfo').openid,
              videoId: this.data.items[index].videoId,
              appid:wx.getAccountInfoSync().miniProgram.appId
            },
            success: res => {
                if(res.data.code == 200){
                  this.data.items.splice(index,1);
                  this.setData({items:this.data.items});
                }
            }
          });
        }
      }
    });
  },
  // button获取用户信息
  loginForUser: function (e) {
    if (e.detail.userInfo) {
      var user = e.detail.userInfo;
      this.setData({
        isShowUserName: true,
        userInfo: e.detail.userInfo,
      })
      user.openid = app.globalData.openid;
      app._saveUserInfo(user);
      this.setData({ userinfo: user });
    } else {
      app._showSettingToast('登陆需要允许授权');
    }
  },
  onShareAppMessage: function () {
    var appInfo = app.globalData.appInfo;
    return {
      title: appInfo.shareTitle,
      path: 'pages/index/index',
      imageUrl:  appInfo.shareIcon
    }
  },
  openMall:function(){
    wx.navigateTo({
      url: '../mall/index',
    });
  },
  openOrder:function(){
    wx.navigateTo({
      url: '../mall/order',
    });
  },
  openSign:function(){
    wx.navigateTo({
      url: '../user/sign',
    });
  },
  openInspire:function(){
    wx.navigateTo({
      url: '../user/inspire',
    });
  }
})